// 此处只有两个API,全局导入API
export default {
  data() {
    return {
      listLoading: false,
      total: 0,
      list: [],
      btnLoading: false,
      dialogFormVisible: false, // 弹出框
      objectCopy: '', // 默认初始化值的副本
      selectobjectCopy: {}, // 单行选中的数据副本
      multipleSelection: [],
      message: ''
    }
  },
  created() {
    // 备份查询条件
    this.listQueryCopy = this._.cloneDeep(this.listQuery)
    // 清空时的副本
    this.objectCopy = this._.cloneDeep(this.object)
    // 初始化数据
    // this.searchList()
  },
  methods: {
    // 根据Id查询数据
    async getDateById(id) {
      const query = {
        id: id,
        plateType: 'gnmpp_fangyuns'
      }
      this.searchListAll(query).then((res) => {
        this.object = res.data.list ? res.data.list[0].object : {}
        this.selectobjectCopy = res.data.list ? res.data.list[0] : {}
      })
    },
    // 基础查询方法
    getList(listQuery) {
      this.listLoading = true
      this.searchListAll(listQuery).then((res) => {
        this.list = res.data.list
        this.total = res.data.total
        this.listLoading = false
      })
    },
    // 查询
    searchList() {
      const listQuery = this.setListQuery(this._.cloneDeep(this.listQuery))
      this.getList(listQuery)
    },
    // 重置
    resetSearch() {
      this.listQuery = this.setListQuery(this._.cloneDeep(this.listQueryCopy))
      this.getList(this.listQuery)
    },
    /**
         * 查询条件的二次处理,前端格式与后台
         */
    setListQuery(query) {
      const newQuery = {
        'newQueryVos': [],
        'pageSize': 10,
        'plateType': 'gnmpp_fangyuns',
        'pageNum': query.pageNum
      }
      this._.forEach(query, function(value, key) {
        if (key !== 'pageNum' && key !== 'pageSize') {
          newQuery.newQueryVos.push({
            'newQueryName': key,
            'newQueryValue': value
          })
        }
      })
      return newQuery
    },
    // 页面跳转
    toPath(url) {
      this.$router.push(url)
    },
    // 选择复选框
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 撤销/发布
    releaseStatus(row, status, message) {
      const data = this._.cloneDeep(row)
      data.object.status = status
      this.$confirm(`此操作将${message}, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.saveNewMediaAll(data).then(() => {
          this.$message({
            type: 'success',
            message: `${message}成功!`
          })
          this.searchList()
        })
      })
    },
    // 删除
    deleteDate(id, message) {
      this.$confirm(`此操作将${message}, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const params = {
          id: id,
          plateType: 'gnmpp_fangyuns'
        }
        this.deleteNewMediaAll(params).then((res) => {
          this.$message({
            type: 'success',
            message: `${message}成功!`
          })
          this.searchList()
        }).catch(() => {
        })
      })
    },
    // 展示dialog == 本页新增数据
    showDialog(message = '') {
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.object = this.objectCopy
        this.$refs.objectForm.resetFields()
        this.objectCopy = {}
        this.message = message
      })
    },
    updateDialog(row, message = '') {
      this.dialogFormVisible = true
      this.selectobjectCopy = row
      this.object = Object.assign({}, this.object, row.object)
      console.log(this.object)
      this.message = message
    },
    // 数据提交
    onSubmit(message = '') {
      let data = {}
      if (this.selectobjectCopy && this.selectobjectCopy.id) {
        this.selectobjectCopy.object = this.object
        data = this._.cloneDeep(this.selectobjectCopy)
      } else {
        data = {
          object: this.object,
          plateType: 'gnmpp_fangyuns'
        }
      }
      // const message = data.id ? '修改视频' : '新增视频'
      console.log(data)
      // return
      this.saveNewMedia(data, message)
      // .then(() => {
      //     this.searchList()
      // })
    },

    // 数据保存方法
    saveNewMedia(data, message) {
      console.log(data)
      // return
      this.$refs['objectForm'].validate((valid) => {
        if (valid) {
          // return new Promise((resolve, reject) => {
          this.$confirm(`此操作将${message}, 是否继续?`, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            this.btnLoading = true
            this.saveNewMediaAll(data).then(() => {
              this.$message({
                type: 'success',
                message: `${message}成功!`
              })
              this.btnLoading = false
              this.dialogFormVisible = false
              this.searchList()
              // resolve(true)
            }).catch(() => {
              this.btnLoading = false
              // reject()
            })
          })
          // })
        }
      })
    },
    // 取消方法
    onCancel() {
      this.dialogFormVisible = false
    },
    // 返回
    onBack() {
      this.$router.go(-1)
    }
  }
}
